package dao.Impl;

import dao.EmployeeRepository;
import entity.Employee;
import utils.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class EmployeeRepositoryImpl implements EmployeeRepository {

    /**
     * 从数据库中获取员工信息表
     */
    @Override
    public  ArrayList<Employee> getEmployeeList(){
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from servicemanagementsystem.employee";
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        Employee employee = null;
        ArrayList<Employee> employeeList = new ArrayList<>();
        try {
            ps = connection.prepareStatement(sql);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer id = resultSet.getInt(1);
                String Name = resultSet.getString(2);
                Integer age = resultSet.getInt(3);
                String job = resultSet.getString(4);
                String addr = resultSet.getString(5);
                String phone = resultSet.getString(6);
                String wechat = resultSet.getString(7);
                String mail = resultSet.getString(8);
                employee = new Employee(id, Name, age,job,addr,phone,wechat,mail);
                employeeList.add(employee);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection, ps, resultSet);
        }
        return employeeList;
    }
    /**
     * 添加员工信息
     */
    @Override
    public boolean addEmployee(Employee employee) {
        Connection connection = null;
        String sql = "insert into servicemanagementsystem.employee (eid, name, age, job,addr,phone,wechat,mail) " +
                "values (?,?,?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, employee.getEid());
            ps.setString(2, employee.getName());
            ps.setInt(3, employee.getAge());
            ps.setString(4, employee.getJob());
            ps.setString(5,employee.getAddr());
            ps.setString(6,employee.getPhone());
            ps.setString(7,employee.getWechat());
            ps.setString(8,employee.getMail());
            int result = ps.executeUpdate();
            if (result == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("error when add employee");
            return false;
        } finally {
            JDBCTools.release(connection, ps, null);
        }
        return false;
    }

    /**
     * 根据员工编号查找数据库返回员工信息对象
     */
    @Override
    public Employee findById(Integer employeeId) {
        Connection connection = null;
        String sql = "select * from servicemanagementsystem.employee where eid=?";
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        Employee employee = null;
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, employeeId);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer id = resultSet.getInt(1);
                String Name = resultSet.getString(2);
                Integer age = resultSet.getInt(3);
                String job = resultSet.getString(4);
                String addr = resultSet.getString(5);
                String phone = resultSet.getString(6);
                String wechat = resultSet.getString(7);
                String mail = resultSet.getString(8);
                employee = new Employee(id, Name, age,job,addr,phone,wechat,mail);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, ps, resultSet);
        }
        return employee;
    }
    /**
     * 修改员工信息
     */
    @Override
    public boolean updateEmployee(Employee employee) {
        Connection connection = null;
        String sql = "update servicemanagementsystem.employee set name=? , age=?, job=?, addr=? ,phone=?,wechat=?,mail=? " +
                "where eid=?";
        PreparedStatement ps = null;
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1, employee.getName());
            ps.setInt(2, employee.getAge());
            ps.setString(3, employee.getJob());
            ps.setString(4,employee.getAddr());
            ps.setString(5,employee.getPhone());
            ps.setString(6,employee.getWechat());
            ps.setString(7,employee.getMail());
            ps.setInt(8, employee.getEid());
            int result = ps.executeUpdate();
            if (result == 1) {
                return true;
            }
        } catch (SQLException e) {
            System.out.println("error when update employee");
            return false;
        } finally {
            JDBCTools.release(connection, ps, null);
        }
        return false;
    }

    /**
     * 根据员工编号删除员工信息
     */
    @Override
    public boolean deleteEmployee(Integer employeeId) {
        Connection connection = null;
        String sql = "delete from servicemanagementsystem.employee where eid = ?";
        PreparedStatement ps = null;
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, employeeId);
            int result = ps.executeUpdate();
            if (result == 1) {
                return true;
            }
        } catch (SQLException e) {
            return false;
        } finally {
            JDBCTools.release(connection, ps, null);
        }
        return false;
    }


}
